<!-- eslint-disable vue/valid-v-slot -->
<!-- eslint-disable vue/no-lone-template -->
<template>
  <div class="main-container">
    <TableBody>
      <template #header>
        <TableHeader ref="tableHeaderRef" :show-filter="false">
          <template #search-content>
            <a-form ref="seachForm" :model="form">
              <a-row :gutter="24">
                <a-col :span="4">
                  <a-form-item field="card" hide-label>
                    <a-input v-model="form.card" placeholder="请输入卡号" allow-clear />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="addTime" hide-label>
                    <a-date-picker
                      class="width100"
                      v-model="form.addTime"
                      :disabledDate="changeTraceEnd"
                      placeholder="请选择开始统计时间"
                      format="YYYY-MM-DD"
                      :shortcuts="[
                        {
                          label: '今日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss').format('YYYY-MM-DD')),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss').format('YYYY-MM-DD'))
                          ),
                        },
                        {
                          label: '昨日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD')),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD'))
                          ),
                        },
                        {
                          label: '七日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(6, 'day')
                              .format('YYYY-MM-DD')),
                            (form.endTime = $dayjs().format('YYYY-MM-DD'))
                          ),
                        },
                        {
                          label: '本月',
                          value: () => (
                            (form.addTime = $dayjs('01 00:00:00', 'DD HH:mm:ss').format(
                              'YYYY-MM-DD'
                            )),
                            (form.endTime = $dayjs().format('YYYY-MM-DD'))
                          ),
                        },
                        {
                          label: '上月',
                          value: () => (
                            (form.addTime = $dayjs(`01 00:00:00`, 'DD HH:mm:ss')
                              .subtract(1, 'month')
                              .format('YYYY-MM-DD')),
                            (form.endTime = $dayjs()
                              .subtract(1, 'month')
                              .endOf('month')
                              .format('YYYY-MM-DD'))
                          ),
                        },
                      ]"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="endTime" hide-label>
                    <a-date-picker
                      class="width100"
                      v-model="form.endTime"
                      :disabledDate="changeTraceStart"
                      placeholder="请选择结束统计时间"
                      :shortcuts="[
                        {
                          label: '今日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            ))
                          ),
                        },
                        {
                          label: '昨日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD')),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD'))
                          ),
                        },
                        {
                          label: '七日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(6, 'day')
                              .format('YYYY-MM-DD')),
                            (form.endTime = $dayjs().format('YYYY-MM-DD'))
                          ),
                        },
                        {
                          label: '本月',
                          value: () => (
                            (form.addTime = $dayjs('01 00:00:00', 'DD HH:mm:ss').format(
                              'YYYY-MM-DD'
                            )),
                            (form.endTime = $dayjs().format('YYYY-MM-DD'))
                          ),
                        },
                        {
                          label: '上月',
                          value: () => (
                            (form.addTime = $dayjs(`01 00:00:00`, 'DD HH:mm:ss')
                              .subtract(1, 'month')
                              .format('YYYY-MM-DD')),
                            (form.endTime = $dayjs()
                              .subtract(1, 'month')
                              .endOf('month')
                              .format('YYYY-MM-DD'))
                          ),
                        },
                      ]"
                    />
                  </a-form-item>
                </a-col>

                <a-col :span="4">
                  <a-form-item hide-label>
                    <a-input-group class="width100">
                      <a-input v-model="form.addRealUsed" placeholder="开始真实已用" />
                      <a-input v-model="form.endRealUsed" placeholder="结束真实已用" />
                    </a-input-group>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item hide-label>
                    <a-input-group class="width100">
                      <a-input v-model="form.addTotalRealUsed" placeholder="开始真实总已用" />
                      <a-input v-model="form.endTotalRealUsed" placeholder="结束真实总已用" />
                    </a-input-group>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item hide-label>
                    <a-input-group class="width100">
                      <a-input v-model="form.addUsed" placeholder="开始使用流量" />
                      <a-input v-model="form.endUsed" placeholder="结束使用流量" />
                    </a-input-group>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item hide-label>
                    <a-input-group class="width100">
                      <a-input v-model="form.addTotalUsed" placeholder="开始总使用流量" />
                      <a-input v-model="form.endTotalUsed" placeholder="结束总使用流量" />
                    </a-input-group>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="btns" hide-label>
                    <a-space :size="15">
                      <a-button
                        type="primary"
                        v-permission="cardDosage"
                        html-type="submit"
                        @click="doRefresh(1)"
                        ><template #icon> <icon-search /> </template>搜索</a-button
                      >
                      <a-button v-permission="cardDosage" @click="resetSeach"
                        ><template #icon>
                          <icon-sync theme="outline" size="14" />
                        </template>
                        <!-- Use the default slot to avoid extra spaces -->
                        <template #default>重置</template></a-button
                      >
                    </a-space>
                  </a-form-item>
                </a-col>
              </a-row>
            </a-form>
          </template>
        </TableHeader>
      </template>
      <template #table-config>
        <a-space :size="15">
          <a-button
            type="outline"
            v-permission="exportCardDosage"
            @click="exportExcelLists"
            :loading="loading"
          >
            <!-- Use the default slot to avoid extra spaces -->
            <template #default>导出</template>
          </a-button>
        </a-space>
        <a-space :size="15">
          <TableConfig
            @update-border="onUpdateBorder"
            @update-striped="onUpdateStriped"
            @refresh="doRefresh"
          />
        </a-space>
      </template>
      <template #default>
        <a-table
          :bordered="{ wrapper: true, cell: tables.bordered }"
          :stripe="(tables.striped as boolean)"
          :loading="tables.tableLoading"
          :data="table.dataList"
          :pagination="false"
          :column-resizable="true"
          :rowKey="rowKey"
          :columns="tableColumns.list"
          table-layout-fixed
          hide-expand-button-on-empty
          :default-expand-all-rows="true"
          :scroll="{ x: 1500 }"
        >
          <template #columns>
            <a-table-column
              v-for="item of tableColumns.list"
              :key="item.key"
              align="left"
              :title="(item.title as string)"
              :width="item.width"
              :data-index="(item.key as string)"
              :fixed="item.fixed"
              :ellipsis="true"
              :tooltip="true"
            >
              <template v-if="item.key === 'time'" #cell="{ record }">
                <span>{{ dateTime(record.time) }}</span>
              </template>
              <template v-if="item.key === 'realUsed'" #cell="{ record }">
                <span>{{ useCommon.numbers1(record.realUsed) }}</span>
              </template>
              <template v-if="item.key === 'used'" #cell="{ record }">
                <span>{{ useCommon.numbers1(record.used) }}</span>
              </template>
              <template v-if="item.key === 'totalUsed'" #cell="{ record }">
                <span>{{ useCommon.numbers1(record.totalUsed) }}</span>
              </template>
              <template v-if="item.key === 'totalRealUsed'" #cell="{ record }">
                <span>{{ useCommon.numbers1(record.totalRealUsed) }}</span>
              </template>
              <template v-if="item.key === 'card'" #cell="{ record }">
                <span>{{ record.card.card }}</span>
              </template>
            </a-table-column>
          </template>
        </a-table>
      </template>
      <template #footer>
        <TableFooter ref="tableFooterRef" :pagination="pagination" />
      </template>
    </TableBody>
  </div>
</template>

<script lang="ts" setup>
  import { cardDosage, exportCardDosage } from '@/api/url'
  import { get } from '@/api/http'
  import { usePagination, useRowKey, useTable } from '@/hooks/table'
  import { onMounted, reactive, ref } from 'vue'

  import useCommonStore from '@/store/modules/common'
  const useCommon = useCommonStore()
  const table = useTable()
  const pagination = usePagination(doRefresh)

  const seachForm = ref()

  const loading = ref(false)

  const form = reactive({
    card: '', //             卡号
    addUsed: '', //           开始已用
    endUsed: '', //           结束已用
    addRealUsed: '', //       开始真实已用
    endRealUsed: '', //       结束真实已用
    addTotalUsed: '', //      开始总已用
    endTotalUsed: '', //      结束总已用
    addTotalRealUsed: '', //  开始真实总已用
    endTotalRealUsed: '', //  结束真实总已用
    addTime: '', //           开始时间
    endTime: '', //           结束时间
  })

  const tables = reactive({
    tableLoading: false,
    bordered: false,
    striped: false,
  })

  const tableColumns: any = reactive({
    list: [
      {
        title: '卡号',
        key: 'card',
        dataIndex: 'card',
        align: 'center',
        checked: true,
      },
      {
        title: '真实已用',
        key: 'realUsed',
        dataIndex: 'realUsed',
        align: 'center',
        checked: true,
      },
      {
        title: '真实总已用',
        key: 'totalRealUsed',
        dataIndex: 'totalRealUsed',
        align: 'center',
        checked: true,
      },
      {
        title: '使用流量',
        key: 'used',
        dataIndex: 'used',
        align: 'center',
        checked: true,
      },
      {
        title: '总使用流量',
        key: 'totalUsed',
        dataIndex: 'totalUsed',
        align: 'center',
        checked: true,
      },

      {
        title: '统计时间',
        key: 'time',
        dataIndex: 'time',
        align: 'center',
        checked: true,
      },
    ],
  })

  const rowKey = useRowKey('id')

  const resetSeach = () => {
    form.addUsed = '' //           开始已用
    form.endUsed = ''
    form.addRealUsed = '' //      开始成功数量
    form.endRealUsed = '' //       结束成功数量
    form.addTotalUsed = '' //       开始失败数量
    form.endTotalUsed = '' //     结束失败数量
    form.addTotalRealUsed = '' //      开始线程数量
    form.endTotalRealUsed = '' //      结束线程数量
    seachForm.value.resetFields()
    doRefresh(1)
  }

  function dateTime(val: any) {
    console.log(val)
    // 时间戳
    let timestamp = val + ''
    // 此处时间戳以毫秒为单位
    let GMT =
      timestamp.substring(0, 4) + '-' + timestamp.substring(4, 6) + '-' + timestamp.substring(6, 8)

    return GMT
  }

  const exportExcelLists = () => {
    loading.value = true
    form.addTime = form.addTime.replace(/\-/g, '')
    form.endTime = form.endTime.replace(/\-/g, '')
    useCommon
      .downLoads(exportCardDosage, form, '流量统计')
      .then(() => {
        loading.value = false
      })
      .catch(() => {
        loading.value = false
      })
  }

  function onUpdateBorder(isBordered: boolean) {
    tables.bordered = isBordered
  }

  function onUpdateStriped(isStriped: boolean) {
    tables.striped = isStriped
  }

  function doRefresh(value?: number) {
    tables.tableLoading = true
    if (value) {
      pagination.page = value
    }
    get({
      url: cardDosage,
      data: {
        ...form,
        addTime: form.addTime.replace(/\-/g, ''),
        endTime: form.endTime.replace(/\-/g, ''),
        page: pagination.page,
        size: pagination.pageSize,
      },
    }).then((res) => {
      tables.tableLoading = false
      table.handleSuccess(res.data.list)
      pagination.setTotalSize((res.data as any).totalSize)
      return res.data.list
    })
  }

  //跟踪API开始时间限制结束时间
  function changeTraceEnd(time: any) {
    return time.getTime() > new Date(form.endTime).getTime() //大于结束时间的不选
  }
  //跟踪API结束时间限制开始时间
  function changeTraceStart(time: any) {
    return time.getTime() < new Date(form.addTime).getTime() //小于开始时间的不选
  }
  onMounted(async () => {
    await doRefresh()
  })
</script>

<style lang="less" scoped>
  :deep(.arco-table-cell-expand-icon) {
    justify-content: center;
  }

  .box {
    overflow: hidden; //超出的文本隐藏
    text-overflow: ellipsis; //用省略号显示
    white-space: nowrap;
  }

  .tabs {
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: 40px;
    margin-bottom: 20px;

    .button {
      box-sizing: border-box;
      padding-bottom: 5px;
    }
  }

  .action {
    color: #009dff;
    border-bottom: 3px solid #009dff;
  }

  .butten {
    padding: 0px 4px;
  }
</style>
